package com.itheima.sc.impl.cms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.cms.SubjectApi;
import com.itheima.sc.mapper.cms.SubjectMapper;
import com.itheima.sc.pojo.cms.Subject;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SubjectImpl implements SubjectApi {

    @Autowired
    private SubjectMapper subjectMapper;

    //根据专题名称分页获取商品专题
    public PageResult pageList(Integer page, Integer pageSize, String keyword) {
        //构建分页查询条件
        Page<Subject> ipage = new Page<>(page, pageSize);
        //构建条件查询
        LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
        //封装专题名称查询条件
        wrapper.like(StrUtil.isNotBlank(keyword), Subject::getTitle, keyword);
        //查询请求
        ipage = subjectMapper.selectPage(ipage, wrapper);
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }

    //查询所有数据
    public List<Subject> listAll() {
        return subjectMapper.selectList(null);
    }
}
